package com.FileBehavior;

import com.alibaba.fastjson.JSONObject;
import com.http.BaiduPanConfig;
import com.http.HttpRequest;
import com.util.SQLHelp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class BaiduUploader {
    // 定时上传 TODO 大概弃用了
    // folderKey, Set -> filePath
    private HashMap<String, HashSet<String>> fileSet = new HashMap<>();

    public static BaiduUploader baiduUploader= null;
    public static BaiduUploader getBaiduUploader(){
        if(baiduUploader == null){
            baiduUploader = new BaiduUploader();
        }
        return baiduUploader;
    }

    public HashMap<String, HashSet<String>> getFileSet() {
        return fileSet;
    }

    public void setFileSet(HashMap<String, HashSet<String>> fileSet) {
        this.fileSet = fileSet;
    }

    public static boolean addFile(String folderKey, String filePath){
        if(!getBaiduUploader().getFileSet().containsKey(folderKey)){
            getBaiduUploader().getFileSet().put(folderKey, new HashSet<>());
        }
        if(getBaiduUploader().getFileSet().get(folderKey).contains(filePath)){
            // 重复
            return false;
        }
        getBaiduUploader().getFileSet().get(folderKey).add(filePath);
        return true;
    }

//    public static void autoUpload() throws IOException {
//        String folderFormat = "select * from folder where folder_key = '%s'";
//        String panFormat = "select * from baiduoauthtable where pan_id = %s";
//        String panFolderFormat = "/apps/new_folder/%s";
//        String panfilePathFormat = "/apps/new_folder/%s/%s";
//        HashMap<String, HashSet<String>> newFileSet = new HashMap<>();
//        for (String folderKey :
//                getBaiduUploader().getFileSet().keySet()) {
//            String sql = String.format(folderFormat, folderKey);
//            ArrayList<HashMap<String, String>> res = SQLHelp.selectQuery(sql);
//            if(res.isEmpty()){
//                continue;
//            }
//            String pan_id = res.get(0).get("pan_id");
//            if(pan_id.equals("0")){
//                continue;
//            }
//            sql = String.format(panFormat, pan_id);
//            res = SQLHelp.selectQuery(sql);
//            if(res.isEmpty()){
//                continue;
//            }
//            // todo upload
//            String accessToken = res.get(0).get("access_token");
//            String url = BaiduPanConfig.getFileUploadUrl(accessToken);
//            // 文件夹的路径
//            // /apps/new_folder/folder_key
//            String panFolder = String.format(panFolderFormat, folderKey);
//            // 创建文件夹
//            JSONObject body = new JSONObject();
//            body.put("path", panFolder);
//            body.put("isdir", "1");
//            // 不覆盖
//            body.put("rtype", 0);
//            String createUrl = BaiduPanConfig.getCreateFolderUrl(accessToken);
//            JSONObject result = HttpRequest.PostRequest(createUrl, body);
//            if(result.get("errno").equals(-7) || result.get("errno").equals(-10)){
//                continue;
//            }
//            for (String filePath : getBaiduUploader().getFileSet().get(folderKey)){
//                File file = new File(filePath);
//                if(!file.exists()) {
//                    continue;
//                }
//                // 文件的路径
//                // /apps/new_folder/folder_key/file.doc
//                String panFilePath = String.format(panfilePathFormat, folderKey, filePath);
//                body = new JSONObject();
//                body.put("path", "/apps/new_folder/");
//                if(false){
//                    // 失败了
//                    if(!newFileSet.containsKey(folderKey)){
//                        newFileSet.put(folderKey, new HashSet<>());
//                    }
//                    newFileSet.get(folderKey).add(filePath);
//                }
//            }
//        }
//        getBaiduUploader().setFileSet(newFileSet);
//    }
}
